package com.sc.syllabus.decoration;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sc.syllabus.R;
import com.sc.syllabus.decoration.bean.HouseDecorationBean;
import com.sc.syllabus.model.TypeId;
import com.sc.syllabus.utils.IPopupSelectListener;
import com.sc.syllabus.utils.PopupListUtils;
import com.sc.syllabus.utils.ToastUtil;
import com.srain.utils.LogUtils;

/**
 * Created by liushuanglong on 2017/9/20.
 * <p>
 * 装修价格
 */
public class EditDecorationActivity extends Activity {

    private static final String TAG = "EditDecorationActivity";
    private static final String EXTRA = "subclass";

    public static void actionStart(Activity activity) {
        Intent intent = new Intent(activity, EditDecorationActivity.class);
        activity.startActivity(intent);
    }

    public static void actionStart(Activity activity, HouseDecorationBean bean) {
        Intent intent = new Intent(activity, EditDecorationActivity.class);
        intent.putExtra(EXTRA, bean);
        activity.startActivity(intent);
    }

    private EditText etTitle, etEPrice, etENum, etAPrice, etANum;
    private TextView eType;
    private TextView btnSub;
    private boolean hasExtra;
    private HouseDecorationBean bean;
    private DecorationModelImpl mModel;
    private int mType = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_decoration);

        initView();
        initData();
    }

    private void initView() {
        etTitle = (EditText) findViewById(R.id.edit_title);
        etEPrice = (EditText) findViewById(R.id.edit_expect_price);
        etENum = (EditText) findViewById(R.id.edit_expect_num);
        etAPrice = (EditText) findViewById(R.id.edit_actual_price);
        etANum = (EditText) findViewById(R.id.edit_actual_num);
        eType = (TextView) findViewById(R.id.edit_type);
        btnSub = (Button) findViewById(R.id.btn_sub);
        btnSub.setOnClickListener(mClickListener);
        eType.setOnClickListener(mClickListener);
    }

    private void initData() {
        mModel = DecorationModelImpl.getInstance(this);
        hasExtra = this.getIntent().hasExtra(EXTRA);
        if (hasExtra) {
            bean = this.getIntent().getParcelableExtra(EXTRA);
            etTitle.setText(bean.getTitle());
            etEPrice.setText(bean.getExpectPrice() + "");
            etENum.setText(bean.getExpectNum() + "");
            etAPrice.setText(bean.getActualPrice() + "");
            etANum.setText(bean.getActualNum() + "");
            eType.setText(TypeId.typeTitles[bean.getType()]);
        }
    }

    private View.OnClickListener mClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_sub:
                    save();
                    break;

                case R.id.edit_type:
                    PopupListUtils popup = new PopupListUtils(EditDecorationActivity.this, R.id.layout, TypeId.typeTitles);
                    popup.setSelect(hasExtra ? bean.getType() : 0);
                    popup.registSelectListener(mPopupListener);
                    popup.show();
                    break;
            }
        }
    };

    private IPopupSelectListener mPopupListener = new IPopupSelectListener() {
        @Override
        public void updateTitle(String title) {
            eType.setText(title);
        }

        @Override
        public void updatePosition(int position) {
            mType = position;
        }
    };

    private void save() {
        int id = 0;
        String title = etTitle.getText().toString().trim();
        if (TextUtils.isEmpty(title)) {
            return;
        }

        String ePrice = etEPrice.getText().toString().trim();
        if (TextUtils.isEmpty(ePrice)) {
            return;
        }

        String eNum = etENum.getText().toString().trim();
        if (TextUtils.isEmpty(eNum)) {
            return;
        }

        String aPrice = etAPrice.getText().toString().trim();
        if (TextUtils.isEmpty(aPrice)) {
            return;
        }

        String aNum = etANum.getText().toString().trim();
        if (TextUtils.isEmpty(aNum)) {
            return;
        }

        if (!hasExtra) {
            bean = new HouseDecorationBean();
        }
        bean.setType(mType);
        bean.setTitle(title);
        bean.setExpectPrice(Double.valueOf(ePrice));
        bean.setExpectNum(Integer.valueOf(eNum));
        bean.setActualPrice(Double.valueOf(aPrice));
        bean.setActualNum(Integer.valueOf(aNum));

        if (hasExtra) {
            LogUtils.i(TAG, "modify --- bean == " + bean.toString());
            id = mModel.modify(bean);
        } else {
            id = (int) mModel.save(bean);
        }

        if (0 == id) {
            ToastUtil.show(EditDecorationActivity.this, "保存失败,请重新保存!");
        } else if (id > 0) {
            ToastUtil.show(EditDecorationActivity.this, "保存成功, id == " + id);
            EditDecorationActivity.this.finish();
        } else if (id < 0) {
            ToastUtil.show(EditDecorationActivity.this, "保存失败, id == " + id);
        }
    }
}
